package com.qf.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qf.baseservice.impl.BaseServiceImpl;
import com.qf.entity.Address;
import com.qf.entity.User;
import com.qf.mapper.IAddressMapper;
import com.qf.service.IAddressService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author：刘亚康
 * @Date：2020/4/11
 * @QQ：50770284
 * @Phone：18235947935
 * @Desc :
 **/
@Slf4j
@Service
public class AddressServiceImpl extends BaseServiceImpl<Address> implements IAddressService {
    @Autowired
    private IAddressMapper addressMapper;
    @Override
    protected BaseMapper<Address> getBaseMapper() {
        return addressMapper;
    }

    /**
     * 根据用户id获取地址列表
     *
     * @param user
     * @return
     */
    @Override
    public List<Address> getAddressListByUserId(User user) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",user.getUserId());
        List<Address> addressList = addressMapper.selectList(wrapper);
        log.info("获取用户id为："+user.getUserId()+"的所有地址为：");
        addressList.forEach(System.out::println);
        return addressList;
    }

    /**
     * 添加用户地址
     *
     * @param user
     * @param address
     * @return
     */
    @Override
    public int insertAdd(User user, Address address) {
        //给地址添加用户id
        address.setUserId(user.getUserId());
        //判断是否是默认地址
        if (address.getIsDefault()!=null && address.getIsDefault()==0) {
            //是默认地址修改其他地址为1
            List<Address> addressList = getAddressListByUserId(user);
            for (Address address1 : addressList) {
                address1.setIsDefault(1);
                addressMapper.updateById(address1);
            }
        }
        int insert = addressMapper.insert(address);
        return insert;
    }

    /**
     * 修改用户地址
     *
     * @param user
     * @param address
     * @return
     */
    @Override
    public int updateAddress(User user, Address address, Integer updateAfterStatusIsDefault) {
        //判断地址是否是null
        if (address.getIsDefault()==null) {
            address.setIsDefault(1);
        }
        //给地址添加用户id
        log.info("给地址添加用户id");
        address.setUserId(user.getUserId());
        int result=0;
        //判断用户之前的地址是不是默认的
        log.info("判断用户之前的地址是不是默认的");
        if (updateAfterStatusIsDefault==0) {//是默认的
            log.info(address.getAddressId()+"修改之前地址是默认的");
            //判断现在是否是默认地址
            if (address.getIsDefault()!=null && address.getIsDefault()==0) {
                log.info(address.getAddressId()+"现在地址还是默认的");
                result=addressMapper.updateById(address);
            }else {//现在不是默认地址
                log.info(address.getAddressId()+"现在地址不是默认的");
                //获取所有地址
                List<Address> addressList = getAddressListByUserId(user);
                for (Address address1 : addressList) {
                    if (!address1.getAddressId().equals(address.getAddressId())) {
                        //找一个与该地址不相等的做默认地址，修改该地址
                        log.info(address1.getAddressId()+"修改该地址为默认地址");
                        address1.setIsDefault(0);
                        addressMapper.updateById(address1);
                        break;
                    }
                }
                //修改要修改的地址
                result=addressMapper.updateById(address);
            }
        }else {//之前不是默认的,直接修改
            log.info(address.getAddressId()+"修改之前地址不是默认的，直接修改");
            result=addressMapper.updateById(address);
        }


        return result;
    }

    /**
     * 删除地址
     *
     * @param addressId
     * @Param user
     * @return
     */
    @Override
    public int deleteAddress(Integer addressId,User user) {
        //判断地址是不是默认地址
        Address address = addressMapper.selectById(addressId);
        int result=0;
        if (address.getIsDefault()==0) {
            log.info("该地址是默认的");
            //获取用户所有地址
            List<Address> addressList = getAddressListByUserId(user);
            for (Address address1 : addressList) {
                if (!address1.getAddressId().equals(address.getAddressId())) {
                    //找一个与该地址不相等的做默认地址，修改该地址为默认地址
                    log.info(address1.getAddressId()+"修改该地址为默认地址");
                    address1.setIsDefault(1);
                    addressMapper.updateById(address1);
                    break;
                }
            }
            log.info("删除该地址"+addressId);
            result=addressMapper.deleteById(addressId);
        }else {
            log.info("该地址不是默认的");
            result=addressMapper.deleteById(addressId);
        }
        return result;
    }
}
